package com.siae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siae.mapper.MessageTemplateMapper;
import com.siae.pojo.MessageTemplate;
import com.siae.pojo.vo.Result;
import com.siae.service.MessageTemplateService;
import com.siae.utils.BizCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageTemplateServiceImpl extends ServiceImpl<MessageTemplateMapper, MessageTemplate> implements MessageTemplateService {
    @Autowired
    private MessageTemplateMapper templateMapper;

    @Override
    public Result<List<MessageTemplate>> getAllMTemplates(String text,String type) {
        try{
            QueryWrapper<MessageTemplate> wrapper = new QueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(text),"text",text)
                    .like(StringUtils.isNotBlank(type),"type",type);
            List<MessageTemplate> list = templateMapper.selectList(wrapper);
            return Result.success(BizCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询消息模板列表失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<MessageTemplate> getMTemplateById(Integer id) {
        try{
            MessageTemplate template = templateMapper.selectById(id);
            return Result.success(BizCode.SUCCESS,template);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询消息模板失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<MessageTemplate> getMTemplateByName(String type) {
        try{
            if(StringUtils.isBlank(type)){
                return Result.fail(BizCode.BLANK_ERROR);
            }
            QueryWrapper<MessageTemplate> wrapper = new QueryWrapper<>();
            wrapper.eq(StringUtils.isNotBlank(type),"type",type);
            MessageTemplate template = templateMapper.selectOne(wrapper);
            return Result.success(BizCode.SUCCESS,template);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询消息模板失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result addMTemplate(MessageTemplate template) {
        try{
            template.setCreatedAt(new Date());
            template.setUpdatedAt(new Date());
            template.setIsDel(0);
            templateMapper.insert(template);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("添加消息模板失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result updateMTemplate(MessageTemplate template) {
        try{
            template.setUpdatedAt(new Date());
            templateMapper.updateById(template);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("修改消息模板失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result deleteMTemplate(Integer id) {
        try{
            templateMapper.deleteById(id);
            return Result.success(BizCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            log.error("删除消息模板失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }
}
